<!-- 模板 -->
<template>
  <div class="root">
    <div class="header_bg"></div>
    <div class="box">
      <p><span>购物车</span><span @click="deleteUserId">清空</span></p>
      <div class="mid" v-for="(item, index) in cartList" :key="index">
        <div class="left">
          <input
            type="checkbox"
            class="choice"
            :checked="item.checked == 0 ? false : true"
            @click="check(item.checked, item.goodsId)"
          />
          <img :src="item.imgUrl" alt="" />
          <div class="text">
            <p>{{ item.name }}</p>
            <p class="select">{{ item.brand }}</p>
            <p class="price">￥{{ item.price }}</p>
          </div>
        </div>

        <div class="pri">
          <van-stepper
            v-model="item.num"
            theme="round"
            button-size="22"
            disable-input
            integer
            @change="getNum(item.num, item.id)"
          />
        </div>
      </div>
      <div class="foot">
        <div class="left">
          <p>
            应付合计：￥<i class="sum">{{ sum }}</i>
          </p>
          <p>用劵可抵扣￥14</p>
        </div>
        <div class="right"><span>去结算</span></div>
      </div>
    </div>
    <div class="recommend">
      <div class="row">
        <h3>猜你喜欢</h3>
        <span>换一批</span>
      </div>

      <div class="recommend_goods">
        <ul>
          <li>
            <img
              src="http://42.192.80.102/Printing man-img/superman1.png"
              alt=""
            />
            <p>马克杯</p>
            <span class="price">￥28</span>
          </li>
          <li>
            <img
              src="http://42.192.80.102/Printing man-img/chaoren1.png"
              alt=""
            />
            <p>随身杯</p>
            <span class="price">￥25</span>
          </li>
          <li>
            <img src="http://42.192.80.102/Printing man-img/card1.png" alt="" />
            <p>鼠标垫</p>
            <span class="price">￥4.55</span>
          </li>
        </ul>
      </div>
    </div>
    <MyFoots></MyFoots>
  </div>
</template>
<script>
import request from "../utils/request.js";
import MyFoots from "./MyFoots.vue";
import { Dialog } from "vant";
import { Toast } from "vant";
Toast.setDefaultOptions({ duration: 2000 });
export default {
  data() {
    return {
      cartList: [],
      sum: 0,
    };
  },
  props: [],
  components: { MyFoots },
  computed: {},
  filters: {},
  methods: {
    //获得商品id 用户id
    getUserId() {
      let str = sessionStorage.getItem("user-login");
      if (!str) {
        return alert("请先登录");
      }
      let userId = JSON.parse(str).data[0].id;
      return userId;
    },
    getGoodsId() {
      let str = sessionStorage.getItem("cart-goodsId");
      if (!str) {
        return alert("请先登录");
      }
      let goodsId = JSON.parse(str);
      console.log(goodsId);
      return goodsId;
    },
    async getByIdCart() {
      let options = {
        url: "users/cart",
        type: "get",
        data: {
          userId: this.getUserId(),
        },
      };
      try {
        let { data: res } = await request(options);
        if (res.meta.status !== 200) {
          return console.log(res.meta.msg);
        }
        console.log(res.meta.msg);
        console.log(res);
        this.cartList = res.data;
        this.sum = res.sum;
        console.log(this.cartList);
      } catch (error) {
        console.log(error);
      }
    },
    //点击勾选
    async check(check, goodsId) {
      console.log(check, goodsId);
      if (check == 0) {
        check = 1;
      } else {
        check = 0;
      }
      let options = {
        url: "users/checked",
        type: "get",
        data: {
          userId: this.getUserId(),
          checked: check,
          goodsId: parseInt(goodsId),
        },
      };
      try {
        let { data: res } = await request(options);
        if (res.meta.status !== 200) {
          return console.log(res.meta.msg);
        }
        console.log(res.meta.msg);
        this.getByIdCart();
      } catch (error) {
        console.log(error);
      }
      console.log(check);
    },
    //数量改变
    async getNum(value, id) {
      console.log(value, id);
      let options = {
        url: "users/add",
        type: "get",
        data: {
          userId: this.getUserId(),
          id,
          number: value,
        },
      };
      try {
        let { data: res } = await request(options);
        if (res.meta.status !== 200) {
          return console.log(res.meta.msg);
        }
        console.log(res.meta.msg);
        this.getByIdCart();
      } catch (error) {
        console.log(error);
      }
    },
    //点击清空
    deleteUserId() {
      Dialog.confirm({
        message: "确认清空吗",
      })
        .then(() => {
          this.delete();
        })
        .catch(() => {
          // on cancel
        });
    },
    //清空
    async delete() {
      let options = {
        url: "users/delete",
        type: "delete",
        data: {
          userId: this.getUserId(),
        },
      };
      try {
        let { data: res } = await request(options);
        if (res.meta.status !== 200) {
          return console.log(res.meta.msg);
        }
        Toast.success(res.meta.msg);
        this.getByIdCart();
      } catch (error) {
        console.log(error);
      }
    },
  },
  created() {},
  mounted() {
    this.getByIdCart();
  },
};
</script>
<style lang="less" scoped>
// @import url(); 引入公共css类
.root {
  // width: 100%;
  .header_bg {
    width: 100%;
    height: 2.3rem;
    background: #ffcf35;
  }
  .box {
    margin: 0.2rem;
    margin-top: -1.06rem;
    padding: 0 0.2rem;
    background: #f9f7f8;
    border-radius: 0.2rem;
    > p {
      display: flex;
      padding: 0.2rem 0;
      justify-content: space-between;
      border-bottom: 0.02rem solid #c4c4c4;
      span {
        &:nth-of-type(1) {
          color: #000000;
          font-size: 0.32rem;
        }
        &:nth-of-type(2) {
          color: #666666;
          font-size: 0.28rem;
        }
      }
    }

    .mid {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0.2rem 0;
      border-bottom: 0.02rem solid #ccc;
      .left {
        display: flex;
        align-items: center;
        img {
          width: 1.2rem;
        }
        > input {
          width: 0.4rem;
          height: 0.4rem;
          border: 0.02rem solid #ddd;
          -webkit-border-radius: 50%;
          border-radius: 50%;
          font-size: 0.4rem;
          margin: 0;
          padding: 0;
          position: relative;
          display: inline-block;
          vertical-align: top;
          cursor: default;
          -webkit-appearance: none;
          -webkit-user-select: none;
          user-select: none;
          -webkit-transition: background-color ease 0.1s;
          transition: background-color ease 0.1s;

          &:checked::after {
            content: "";
            top: 0.1rem;
            left: 0.1rem;
            position: absolute;
            background: transparent;
            border: #fff solid 0.04rem;
            border-top: none;
            border-right: none;
            height: 0.08rem;
            width: 0.15rem;
            -moz-transform: rotate(-45deg);
            -ms-transform: rotate(-45deg);
            -webkit-transform: rotate(-45deg);
            transform: rotate(-45deg);
          }
          &:checked {
            background-color: red;
          }
        }

        > img {
          flex: 1;
          margin-left: 0.2rem;
        }
        .text {
          margin-left: 0.2rem;
          > p {
            font-size: 0.28rem;
            color: #333;
            word-break: break-all;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            &:nth-of-type(2) {
              padding: 0.1rem 0;
              color: #999;
            }
          }
          .price {
            color: rgb(190, 3, 3);
          }
        }
      }
      .pri {
        display: flex;
        width: 20%;
        justify-content: end;
        /deep/.van-stepper {
          button {
            color: FFFFFF;
            background: #ffcf35;
            border: 1px solid #ffcf35;
            width: 0.4rem !important;
            height: 0.4rem !important;
          }
          input {
            font-size: 0.24rem;
            width: 0.4rem;
            height: 0.4rem;
          }
        }
      }
    }
    .foot {
      color: #fff;
      background: #f9f7f8;
      display: flex;
      justify-content: space-between;
      width: 100%;
      height: 1.5rem;
      line-height: 1.5rem;
      .left {
        display: flex;
        flex-direction: column;
        line-height: 1.5;
        padding: 0.2rem 0;
        p:nth-of-type(1) {
          font-size: 0.28rem;
          color: #3a3a3a;
          i {
            font-style: normal;
            font-size: 0.28rem;
          }
        }
        p:nth-of-type(2) {
          font-size: 0.28rem;
          color: #e05e2d;
          width: 2.2rem;
          border: 0.02rem solid #e05e2d;
        }
      }
      .right {
        background: #ef5821;
        margin-top: 0.4rem;
        padding: 0 0.4rem;
        height: 0.8rem;
        line-height: 0.8rem;
        border-radius: 0.6rem;
        span {
          font-size: 0.26rem;
        }
      }
    }
  }
  .recommend {
    margin-top: 0.32rem;
    display: flex;
    flex-direction: column;
    padding: 0 0.2rem;
    .row {
      display: flex;
      justify-content: space-between;
      h3 {
        font-size: 0.32rem;
        font-weight: 800;
        margin-bottom: 0.2rem;
      }
      span {
        color: #ffcf35;
        font-size: 0.22rem;
      }
    }

    .recommend_goods {
      ul {
        display: flex;
        align-items: center;
        justify-content: space-between;
        li {
          display: flex;
          flex-direction: column;
          text-align: center;
          img {
            width: 2.26rem;
            height: 2.08rem;
          }
          p {
            padding-top: 0.05rem;
            font-size: 0.22rem;
          }
          .price {
            font-size: 0.3rem;
            padding-top: 0.2rem;
            color: #333333;
          }
        }
      }
    }
  }
}
</style>
